package com.xiguanlezz.cn.course3;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.*;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.junit.Test;

import java.io.IOException;

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;


/**
 * @Author Chen Jie
 * @Date 2024/10/20 7:11
 * @Description
 * @Version 1.0
 */
public class NearRealTimeTest {
    @Test
    public void testNearRealTime() throws IOException {
        Directory dir = new RAMDirectory();
        IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer());
        IndexWriter writer = new IndexWriter(dir, config);

        for (int i = 0; i < 10; i++) {
            Document doc = new Document();
            doc.add(new StringField("id", "" + i, Field.Store.NO));
            doc.add(new TextField("text", "This is text", Field.Store.NO));
            writer.addDocument(doc);
        }
        // writer.commit() writer.close()

        DirectoryReader reader = DirectoryReader.open(writer);
        IndexSearcher searcher = new IndexSearcher(reader);

        Query query = new TermQuery(new Term("text", "text"));
        TopDocs docs = searcher.search(query, 1);
        assertEquals(10, docs.totalHits);

        writer.deleteDocuments(new Term("id", "7"));

        Document doc = new Document();
        doc.add(new StringField("id", "11", Field.Store.NO));
        doc.add(new TextField("text", "This is bbb", Field.Store.NO));
        writer.addDocument(doc);

        IndexReader newReader = DirectoryReader.openIfChanged(reader);
        assertFalse(reader == newReader);
        reader.close();
        searcher = new IndexSearcher(newReader);

        TopDocs hits = searcher.search(query, 10);
        assertEquals(9, hits.totalHits);
        query = new TermQuery(new Term("text", "bbb"));
        hits = searcher.search(query, 1);
        assertEquals(1, hits.totalHits);

        newReader.close();
        writer.close();
    }
}
